package com.happok.onlineclass.usermanagement.setting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.happok.onlineclass.usermanagement.common.exception.DisableException;
import com.happok.onlineclass.usermanagement.common.utils.Sm4Util;
import com.happok.onlineclass.usermanagement.setting.dto.SmsEnableDTO;
import com.happok.onlineclass.usermanagement.setting.dto.SmsSettingDTO;
import com.happok.onlineclass.usermanagement.setting.dto.SmsSettingInfoDTO;
import com.happok.onlineclass.usermanagement.setting.po.SmsPO;
import com.happok.onlineclass.usermanagement.setting.service.SmsDaoService;
import com.happok.onlineclass.usermanagement.setting.service.SmsService;
import com.happok.onlineclass.usermanagement.setting.utils.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 接口分类                        yapi 分类文件夹
 * 功能说明
 *
 * @author : chenac
 * @module 项目名称/模块名          yapi 项目组/子项目
 * @date : 2021/10/12 11:20
 */
@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private SmsDaoService smsDaoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSmsSetting(SmsSettingDTO smsSetting) {
        smsDaoService.updateSmsSetting(smsSetting);
        SmsPO enableSmsSetting = smsDaoService.getEnableSmsSetting();
        if (!ObjectUtils.isEmpty(enableSmsSetting.getAccessKeySecret())){
            enableSmsSetting.setAccessKeySecret(Sm4Util.decrypt(enableSmsSetting.getAccessKeySecret()));
        }
        if (!ObjectUtils.isEmpty(enableSmsSetting) &&
                enableSmsSetting.getEnable() &&
                enableSmsSetting.getSmsPlatform().equals(smsSetting.getSmsPlatform())) {
            // 测试连接短信服务器
            SmsUtil.connectToAliSms(enableSmsSetting.getAccessKeyId(), enableSmsSetting.getAccessKeySecret(), enableSmsSetting.getDomainName());
        }
    }

    @Override
    public List<SmsSettingInfoDTO> listSmsSetting() {
        List<SmsPO> smsPOS = smsDaoService.listSmsSetting();
        return BeanUtil.copyToList(smsPOS, SmsSettingInfoDTO.class);
    }

    @Override
    public void smsEnable(SmsEnableDTO smsEnable) {
        List<SmsPO> smsPOS = smsDaoService.listSmsSetting();
        if (smsEnable.getEnable()) {
            for (SmsPO smsPO : smsPOS) {
                if (smsEnable.getSmsPlatform().equals(smsPO.getSmsPlatform())) {
                    // 测试连接短信服务器
                    SmsUtil.connectToAliSms(smsPO.getAccessKeyId(), smsPO.getAccessKeySecret(), smsPO.getDomainName());
                    smsDaoService.smsEnable(smsEnable);
                }
            }
        } else {
            smsDaoService.smsEnable(smsEnable);
        }
    }

    @Override
    public void checkAndConnectSms() {
        List<SmsPO> smsPOS = smsDaoService.listSmsSetting();
        boolean isEnabled = false;
        for (SmsPO smsPO : smsPOS) {
            if (smsPO.getEnable()) {
                // 测试连接邮箱服务器
                isEnabled = true;
                SmsUtil.connectToAliSms(smsPO.getAccessKeyId(), smsPO.getAccessKeySecret(), smsPO.getDomainName());
            }
        }
        if (!isEnabled) {
            throw new DisableException("短信平台设置异常");
        }
    }

    @Override
    public SmsPO getEnableSmsSetting() {
        return smsDaoService.getEnableSmsSetting();
    }
}